import {
  wxRequest
} from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    devId: '',
    imgUrl: app.globalData.imgUrl,
    device: {},
    typeIndex: 0,
    deviceList: [],
    typeId: '',
    uploadUrl: wx.getStorageSync('uploadUrl'),
    hasNextPage: false,
    pageNo: 1,
    isAll: false,
    num: 0,
    isLongTap: false,
    startTime: 0,
    endTime: 0,
    checkList: []
  },

  onShow: function (options) {
    this.setData({
      typeId: wx.getStorageSync('currentTypeId')
    })
    this.getDeviceList()
  },

  onLoad: function (options) {
    if (options.devId) {
      this.setData({
        devId: options.devId,
        isAll: false
      })
      return
    }
    if (wx.getStorageSync('devId')) {
      this.setData({
        devId: wx.getStorageSync('devId'),
        isAll: true
      })
      return
    }
    this.setData({
      isAll: true
    })
  },

  goToReplace() {
    wx.showModal({
      title: '提示',
      content: '是否要去换料？',
      success(res) {
        if (res.confirm) {
          let devId = wx.getStorageSync('devId')
          if (devId) {
            wx.setStorageSync('devId', devId)
          }
          wx.navigateTo({
            url: '/pages/color_replace_do/index',
          })
        }
      }
    })
  },

  //下拉刷新
  onPullDownRefresh: function () {
    this.refreshData();
    wx.stopPullDownRefresh()
  },

  refreshData: function () {
    this.setData({
      pageNo: 1
    })
    this.getDeviceList()
  },



  //获取记录列表
  dyeRecord: function (isAll) {
    let params = {}
    if (!isAll || isAll == undefined || !this.data.isAll) {
      params = {
        devId: this.data.deviceList[this.data.typeIndex].id,
        pageNo: this.data.pageNo || 1,
        pageSize: 50,
        keyword: this.data.keyword,
      }
    } else {
      params = {
        pageNo: this.data.pageNo,
        pageSize: 50,
        keyword: this.data.keyword,
      }
    }

    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/dyeRecord/findDyeList',
      method: 'POST',
      data: params,
    }, (res) => {
      let list = res.list;
      let num = res.page.totalSize
      if (this.data.pageNo !== 1) {
        list = this.data.recodeList.concat(list);
      }
      list.forEach((item) => {
        item.isChecked = false
      })
      if (this.data.listType.filter(item => item.devId == this.data.deviceList[this.data.typeIndex].id).length > 0) {
        let deviceNote = this.data.listType.filter(item => item.devId == this.data.deviceList[this.data.typeIndex].id)[0].note
        let isMini = deviceNote == '31_ky_rgj_2' || deviceNote == '31_ky_rgj_4'
        wx.setStorageSync('isMini', isMini)
      }

      this.setData({
        recodeList: list,
        hasNextPage: res.page.hasNextPage,
        pageNo: res.page.pageNo,
        num: num
      });
      console.log(res)
    })
  },







  //触底时，加载更多
  onReachBottom: function () {
    if (this.data.hasNextPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.dyeRecord();
    }
  },

  //搜索关键词
  seachInputConfirm: function (e) {
    this.setData({
      pageNo: 1,
      keyword: e.detail.value
    })
    this.dyeRecord();
  },

  seachBtn: function () {
    this.setData({
      pageNo: 1,
      keyword: this.data.keyword
    })
    this.dyeRecord();
  },

  seachInputVal: function (e) {
    var inputval = e.detail.value;
    console.log(e)
    this.setData({
      keyword: inputval
    })
  },

  //清空
  clearInput: function () {
    this.setData({
      keyword: ''
    })
    this.dyeRecord();
  },

  handleTouchStart(e) {
    this.data.startTime = e.timeStamp;
  },
  handleTouchEnd(e) {
    this.data.endTime = e.timeStamp;
  },

  handleLongTap(e) {
    this.data.isLongTap = !this.data.isLongTap
    this.setData({
      isLongTap: this.data.isLongTap
    })
    console.log(this.data.isLongTap);
  },

  //详情
  toDetail: function (e) {
    if (this.data.endTime - this.data.startTime < 350 && !this.data.isLongTap) {
      let userInfo = wx.getStorageSync('userInfo');
      if (userInfo && userInfo.orgType != 3 && userInfo.orgType != 1) {
        return;
      }
      var item = e.currentTarget.dataset.item
      wx.navigateTo({
        url: '/pages/color_residue/detail?id=' + item.id,
      })
    } else {
      console.log(e);
      let id = e.currentTarget.dataset.item.id
      this.data.recodeList.findIndex((item, index) => {
        if (item.id == id) {
          this.data.recodeList[index].isChecked = !this.data.recodeList[index].isChecked
        }
      })
      let idx = this.data.checkList.findIndex((item, index) => item == id)
      if (idx == -1) {
        this.data.checkList.push(id)
      } else {
        this.data.checkList.splice(idx, 1);
      }
      console.log(this.data.checkList);
      this.setData({
        recodeList: this.data.recodeList,
        checkList: this.data.checkList
      })
    }
  },

  toCreate() {
    let userInfo = wx.getStorageSync('userInfo');
    if (userInfo && userInfo.orgType != 3 && userInfo.orgType != 1) {
      wx.showToast({
        icon: 'error',
        title: '账户无权限使用',
      })
      return;
    }
    wx.setStorageSync('checkList', this.data.checkList)
    wx.navigateTo({
      url: '/pages/color_residue/detail?id=' + this.data.checkList[0]
    })
  },

  bindPickerChange(e) {
    var val = e.detail.value
    console.log(e);
    this.data.recodeList = []
    this.setData({
      typeIndex: val,
      recodeList: [],
      pageNo: 0
    }, () => {
      if (val == 0) {
        this.dyeRecord(true);
      } else {
        this.dyeRecord(false);
      }
    })
  },

  getDeviceList: function () {
    var params = {
      keyword: this.data.keyword,
      pageNo: 1,
      typeId: this.data.typeId,
      pageSize: 5000,
      state1: 2,
      noCountDye: 1
    }

    if (wx.getStorageSync('orgType') == 3) {
      params['stoId'] = wx.getStorageSync('userInfo').orgId
    }

    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/device/findList',
      method: 'POST',
      data: params
    }, (res) => {
      console.log(res);
      let list = res.list.map(item => {
        if (item.stoName) {
          item.note += ` - ${item.stoName}`;
        }

        if (item.stoCreateName) {
          item.note += ` - ${item.stoCreateName}`;
        }
        return item;
      });
      let all = {
        note: '全部'
      }
      list.unshift(all)
      let typeIndex = list.findIndex(item => item.id == this.data.devId);
      if (typeIndex == -1 || this.data.isAll) {
        typeIndex = 0;
      }
      this.getListType()
      this.setData({
        deviceList: list,
        typeIndex
      }, () => {
        // if (list.length > 0) {
        //   this.dyeRecord()
        // }
      });
    })
  },

  getListType: function () {
    var params = {
      keyword: '',
      pageNo: 1,
      pageSize: 100
    }

    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/device/findTypeList',
      method: 'POST',
      data: params
    }, (res) => {
      let list = res.list;
      this.dyeRecord()
      this.setData({
        listType: list
      });
    })
  },

  onHide: function (e) {
    this.data.recodeList.map((item, index) => {
      if (item.isChecked) {
        item.isChecked = false
      }
    })
    this.setData({
      isLongTap: false,
      recodeList: this.data.recodeList,
      checkList: []
    })
  }


})